package native_config

import (
	"context"
	"errors"
	"fmt"
	"gitee.com/vrv_media/go-micro-framework/registry"
	"strings"
)

// 本地配置文件的服务发现

type NativeConfigDiscovery struct {
	ConfigPath string
}

func NewNativeConfigDiscovery(configPath string) *NativeConfigDiscovery {
	return &NativeConfigDiscovery{ConfigPath: configPath}
}

func (d *NativeConfigDiscovery) GetService(_ context.Context, serviceName string) ([]*registry.ServiceInstance, error) {
	if d.ConfigPath == "" {
		return nil, errors.New("ConfigPath cannot be empty")
	}
	configs := strings.Split(d.ConfigPath, ",")
	instances := make([]*registry.ServiceInstance, 0, len(configs))
	for index, config := range configs {
		if config == "" {
			continue
		}
		instance := &registry.ServiceInstance{
			ID:        fmt.Sprintf("%d", index),
			Name:      serviceName,
			Version:   "v1.0",
			Endpoints: []string{config},
			Tags:      []string{},
		}
		instances = append(instances, instance)
	}
	return instances, nil
}

func (d *NativeConfigDiscovery) Watch(_ context.Context, _ string) (registry.Watcher, error) {
	return nil, nil
}
